<?php

class Page_Fe_Products_Step2 extends Page {

    protected $tableObject;

    function __construct() {
        self::$TPL->appendCssFile('wedding/step2');
        self::$TPL->appendJsFile('wedding/step2');

        parent::__construct();
    }

    public function defaultAction() {
		//insert dates from session
		$cart = self::$S->Get('cart');
		if (!$cart || !is_array($cart)) {
			//redirect to home page
			die;
		}

		$formData = self::$S->Get('formData');
		if(!empty($formData)) {
			self::$TPL->assign('formData', $formData);
		}

		$memoryIdObj = new Db_Products_MemoryList(cookie(MEMORY_LIST_NAME_KEY), 'ml_memory_identifier');
		//create new order
		$orderId = self::$S->Get('orderId');
		$delivery = self::$S->Get('delivery');
		self::$TPL->assign('deliveryCountryId', $delivery['countryId']);
		self::$TPL->assign('phoneNumber',self::$SETTINGS['phone_number']);

		$orderObj = new Db_Orders($orderId);
		$orderObj->or_status = 1;
		$orderObj->or_delivery_method_id = (isset($delivery['deliveryId']) ? $delivery['deliveryId'] : null);
		$orderObj->or_country_id = (isset($delivery['countryId']) ? $delivery['countryId'] : null);
		$orderObj->or_memory_id = $memoryIdObj->objectArray->id;
		$orderObj->save();
		self::$S->Set('orderId', $orderObj->id);
		$orderId = $orderObj->id;

		$salutations = Db_Salutation::findAll();
		self::$TPL->assign('salutations', $salutations);

		$orderAddress = Db_OrderAddress::getUserFormDataByOrderId($orderId);
		$countOrderAddress = (!empty($orderAddress) ? count($orderAddress) : null);
		self::$TPL->assign('countOrderAddress', $countOrderAddress);
		if(!empty($orderAddress)) {
			$orderAddressResult = (isset($orderAddress['general']) ? $orderAddress['general'] : (isset($orderAddress['invoice']) ? $orderAddress['invoice'] : null) );
			self::$TPL->assign('generalOrderAddress', $orderAddressResult);
			if(isset($orderAddress['delivery'])) {
				self::$TPL->assign('deliveryOrderAddress', $orderAddress['delivery']);
			}
		}

		$projectCountriesDeliveryMethods = Db_Locale_ProjectCountriesDeliveryMethods::getCountriesDeliveryMethodsByProjectId(self::$PROJECT['id']);
		self::$TPL->assign('projectCountriesDeliveryMethods', $projectCountriesDeliveryMethods);

		$orderProductsObj = new Db_OrderProducts();
		//delete from order products
		$orderProductsObj->deleteByField('op_order_id', $orderId, 0);

		//insert cart in order_products
		foreach ($cart as $memoryProductId => $value) {
			$memoryProductId = Utils::memoryProductIdDecode($memoryProductId);
			$productId = Utils::memoryProductIdDecode($value['productId']);
			$orderProductsObj->id = null;
			$orderProductsObj->op_order_id = $orderId;
			$orderProductsObj->op_product_id = $productId;
			$orderProductsObj->op_theme_id = $value['themeId'];
			$orderProductsObj->op_memory_list_product_id = $memoryProductId;
			$orderProductsObj->op_price_id = $value['priceId'];
			$orderProductsObj->op_amount = (isset($value['amount']) ? $value['amount'] : null);
			$orderProductsObj->op_paper_type_id = $value['paperTypesId'];
			$orderProductsObj->save();
			if (isset($value['envelopeId']) && $value['envelopeId']) {
				$parentId = $orderProductsObj->id;
				$orderProductsObj->id = null;
				$orderProductsObj->op_order_id = $orderId;
				$orderProductsObj->op_product_id = $value['envelopeId'];
				$orderProductsObj->op_theme_id = $value['envelopeThemeId'];
				$orderProductsObj->op_price_id = $value['envelopePriceId'];
				$orderProductsObj->op_amount = $value['envelopeAmount'];
				$orderProductsObj->op_paper_type_id = $value['paperTypesId'];
				$orderProductsObj->op_parent_id = $parentId;
				$orderProductsObj->save();
			}
		}

        self::$TPL->setBodyTemplate('step2');
        return array(
            'type' => 'display',
        );
    }
}